﻿Option Explicit On
'Imports SAPbouiCOM.Framework
Imports System.Windows.Forms
Imports System.IO


Public Class clsEventHandler
    Dim WithEvents SBO_Application As SAPbouiCOM.Application ' holds connection with SBO
    Dim p_oDICompany As New SAPbobsCOM.Company

    Public Sub New(ByRef oApplication As SAPbouiCOM.Application, ByRef oCompany As SAPbobsCOM.Company)
        Dim sFuncName As String = String.Empty
        Try
            sFuncName = "Class_Initialize()"
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Starting function", sFuncName)

            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Retriving SBO Application handle", sFuncName)
            SBO_Application = oApplication
            p_oDICompany = oCompany

            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS", sFuncName)
        Catch exc As Exception
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR", sFuncName)
            Call WriteToLogFile(exc.Message, sFuncName)
        End Try
    End Sub

    Public Function SetApplication(ByRef sErrDesc As String) As Long
        ' **********************************************************************************
        '   Function   :    SetApplication()
        '   Purpose    :    This function will be calling to initialize the default settings
        '                   such as Retrieving the Company Default settings, Creating Menus, and
        '                   Initialize the Event Filters
        '               
        '   Parameters :    ByRef sErrDesc AS string
        '                       sErrDesc = Error Description to be returned to calling function
        '   Return     :    0 - FAILURE
        '                   1 - SUCCESS
        ' **********************************************************************************
        Dim sFuncName As String = String.Empty

        Try
            sFuncName = "SetApplication()"
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Starting Function", sFuncName)

            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling SetMenus()", sFuncName)
            If SetMenus(sErrDesc) <> RTN_SUCCESS Then Throw New ArgumentException(sErrDesc)

            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling SetFilters()", sFuncName)
            If SetFilters(sErrDesc) <> RTN_SUCCESS Then Throw New ArgumentException(sErrDesc)

            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS", sFuncName)
            SetApplication = RTN_SUCCESS
        Catch exc As Exception
            sErrDesc = exc.Message
            Call WriteToLogFile(exc.Message, sFuncName)
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR", sFuncName)
            SetApplication = RTN_ERROR
        End Try
    End Function

    Private Function SetMenus(ByRef sErrDesc As String) As Long
        ' **********************************************************************************
        '   Function   :    SetMenus()
        '   Purpose    :    This function will be gathering to create the customized menu
        '               
        '   Parameters :    ByRef sErrDesc AS string
        '                       sErrDesc = Error Description to be returned to calling function
        '   Return     :    0 - FAILURE
        '                   1 - SUCCESS
        ' **********************************************************************************
        Dim sFuncName As String = String.Empty
        ' Dim oMenuItem As SAPbouiCOM.MenuItem
        Try
            sFuncName = "SetMenus()"
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Starting Function", sFuncName)

            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS", sFuncName)
            SetMenus = RTN_SUCCESS
        Catch ex As Exception
            sErrDesc = ex.Message
            Call WriteToLogFile(ex.Message, sFuncName)
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR", sFuncName)
            SetMenus = RTN_ERROR
        End Try
    End Function

    Private Function SetFilters(ByRef sErrDesc As String) As Long

        ' **********************************************************************************
        '   Function   :    SetFilters()
        '   Purpose    :    This function will be gathering to declare the event filter 
        '                   before starting the AddOn Application
        '               
        '   Parameters :    ByRef sErrDesc AS string
        '                       sErrDesc = Error Description to be returned to calling function
        '   Return     :    0 - FAILURE
        '                   1 - SUCCESS
        ' **********************************************************************************

        Dim oFilters As SAPbouiCOM.EventFilters
        Dim oFilter As SAPbouiCOM.EventFilter
        Dim sFuncName As String = String.Empty

        Try
            sFuncName = "SetFilters()"
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Starting function", sFuncName)

            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Initializing EventFilters object", sFuncName)
            oFilters = New SAPbouiCOM.EventFilters



            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Adding filters", sFuncName)
            SBO_Application.SetFilter(oFilters)

            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS", sFuncName)
            SetFilters = RTN_SUCCESS
        Catch exc As Exception
            sErrDesc = exc.Message
            Call WriteToLogFile(sErrDesc, sFuncName)
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR", sFuncName)
            SetFilters = RTN_ERROR
        End Try
    End Function

    Private Sub SBO_Application_AppEvent(ByVal EventType As SAPbouiCOM.BoAppEventTypes) Handles SBO_Application.AppEvent
        ' **********************************************************************************
        '   Function   :    SBO_Application_AppEvent()
        '   Purpose    :    This function will be handling the SAP Application Event
        '               
        '   Parameters :    ByVal EventType As SAPbouiCOM.BoAppEventTypes
        '                       EventType = set the SAP UI Application Eveny Object        
        ' **********************************************************************************
        Dim sFuncName As String = String.Empty
        Dim sErrDesc As String = String.Empty
        Dim sMessage As String = String.Empty

        Try
            sFuncName = "SBO_Application_AppEvent()"
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Starting function", sFuncName)

            Select Case EventType
                Case SAPbouiCOM.BoAppEventTypes.aet_CompanyChanged, SAPbouiCOM.BoAppEventTypes.aet_ShutDown, SAPbouiCOM.BoAppEventTypes.aet_ServerTerminition
                    sMessage = String.Format("Please wait for a while to disconnect the AddOn {0} ....", System.Windows.Forms.Application.ProductName)
                    p_oSBOApplication.SetStatusBarMessage(sMessage, SAPbouiCOM.BoMessageTime.bmt_Medium, False)
                    End
            End Select

            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS", sFuncName)
        Catch ex As Exception
            sErrDesc = ex.Message
            WriteToLogFile(sErrDesc, sFuncName)
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR", sFuncName)
            ShowErr(sErrDesc)
        Finally
            GC.Collect()  'Forces garbage collection of all generations.
        End Try
    End Sub

    Private Sub SBO_Application_MenuEvent(ByRef pVal As SAPbouiCOM.MenuEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.MenuEvent
        ' **********************************************************************************
        '   Function   :    SBO_Application_MenuEvent()
        '   Purpose    :    This function will be handling the SAP Menu Event
        '               
        '   Parameters :    ByRef pVal As SAPbouiCOM.MenuEvent
        '                       pVal = set the SAP UI MenuEvent Object
        '                   ByRef BubbleEvent As Boolean
        '                       BubbleEvent = set the True/False        
        ' **********************************************************************************
        Dim oForm As SAPbouiCOM.Form = Nothing
        Dim sErrDesc As String = String.Empty
        Dim sFuncName As String = String.Empty

        Try
            sFuncName = "SBO_Application_MenuEvent()"
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Starting function", sFuncName)

            If Not p_oDICompany.Connected Then
                If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling ConnectDICompSSO()", sFuncName)
                If ConnectDICompSSO(p_oDICompany, sErrDesc) <> RTN_SUCCESS Then Throw New ArgumentException(sErrDesc)
            End If

            If pVal.BeforeAction = False Then
                Select Case pVal.MenuUID
                    Case "SBS"
                        Try
                            LoadFromXML("StockBudget.srf", SBO_Application)
                            oForm = SBO_Application.Forms.Item("SBS")
                            StockBudget_Binding(oForm, p_oDICompany, p_oSBOApplication, sErrDesc)

                            oForm.Visible = True
                            Exit Try

                        Catch ex As Exception
                            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                            BubbleEvent = False
                        End Try
                        Exit Sub

                    Case "3045646"
                        oForm = p_oSBOApplication.Forms.ActiveForm
                        If oForm.UniqueID = "SBS" Then
                            Dim omatrix As SAPbouiCOM.Matrix = oForm.Items.Item("Item_3").Specific
                            Dim DeleteFlag As Boolean = False
                            omatrix = oForm.Items.Item("Item_3").Specific

                            For mjs As Integer = 1 To omatrix.RowCount
                                If mjs <= omatrix.RowCount Then
                                    If omatrix.IsRowSelected(mjs) = True Then
                                        If Not String.IsNullOrEmpty(omatrix.Columns.Item("Col_1").Cells.Item(mjs).Specific.String) Then
                                            omatrix.DeleteRow(mjs)
                                            DeleteFlag = True
                                            mjs = mjs - 1
                                        Else
                                            p_oSBOApplication.StatusBar.SetText("Can`t delete the empty row ........ !", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
                                        End If
                                    End If
                                End If
                            Next mjs
                            If DeleteFlag = True Then
                                For mjs As Integer = 1 To omatrix.RowCount
                                    omatrix.Columns.Item("#").Cells.Item(mjs).Specific.string = mjs
                                Next
                                If oForm.Mode = SAPbouiCOM.BoFormMode.fm_OK_MODE Then
                                    oForm.Mode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE
                                End If
                            End If

                        End If

                    Case "1290", "1288", "1289", "1291"
                        oForm = p_oSBOApplication.Forms.ActiveForm
                        If oForm.UniqueID = "SBS" Then
                            Dim omatrix As SAPbouiCOM.Matrix = oForm.Items.Item("Item_3").Specific
                            If Not String.IsNullOrEmpty(omatrix.Columns.Item("Col_0").Cells.Item(omatrix.RowCount).Specific.String) Then
                                p_iRowCount = omatrix.RowCount
                            Else
                                p_iRowCount = omatrix.RowCount - 1
                            End If

                        End If

                    Case "1281"
                        oForm = p_oSBOApplication.Forms.ActiveForm
                        If oForm.UniqueID = "SBS" Then
                            oForm.Items.Item("Item_13").Enabled = True
                        End If

                    Case "1282"

                        oForm = SBO_Application.Forms.Item("SBS")
                        StockBudget_Binding(oForm, p_oDICompany, p_oSBOApplication, sErrDesc)
                        oForm.Items.Item("Item_13").Enabled = False

                End Select
            End If

            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS", sFuncName)
        Catch exc As Exception
            BubbleEvent = False
            ShowErr(exc.Message)
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR", sFuncName)
            WriteToLogFile(Err.Description, sFuncName)
        End Try
    End Sub

    Private Sub SBO_Application_ItemEvent(ByVal FormUID As String, ByRef pVal As SAPbouiCOM.ItemEvent, _
            ByRef BubbleEvent As Boolean) Handles SBO_Application.ItemEvent
        ' **********************************************************************************
        '   Function   :    SBO_Application_ItemEvent()
        '   Purpose    :    This function will be handling the SAP Menu Event
        '               
        '   Parameters :    ByVal FormUID As String
        '                       FormUID = set the FormUID
        '                   ByRef pVal As SAPbouiCOM.ItemEvent
        '                       pVal = set the SAP UI ItemEvent Object
        '                   ByRef BubbleEvent As Boolean
        '                       BubbleEvent = set the True/False        
        ' **********************************************************************************

        Dim sErrDesc As String = String.Empty
        Dim sFuncName As String = String.Empty
        Dim p_oDVJE As DataView = Nothing
        Dim oDTDistinct As DataTable = Nothing
        Dim oDTRowFilter As DataTable = Nothing

        Try
            sFuncName = "SBO_Application_ItemEvent()"
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Starting function", sFuncName)

            If Not IsNothing(p_oDICompany) Then
                If Not p_oDICompany.Connected Then
                    If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling ConnectDICompSSO()", sFuncName)
                    If ConnectDICompSSO(p_oDICompany, sErrDesc) <> RTN_SUCCESS Then Throw New ArgumentException(sErrDesc)
                End If
            End If

            If pVal.BeforeAction = False Then

                Select Case pVal.FormUID
                    Case "SBS"
                        If pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED And pVal.FormMode <> SAPbouiCOM.BoFormMode.fm_FIND_MODE Then
                            If pVal.ItemUID = "20" Then
                                Dim oForm As SAPbouiCOM.Form = p_oSBOApplication.Forms.ActiveForm
                                sFuncName = "'Browse' Button Click "
                                If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling File Open Function", sFuncName)
                                Dim oMatrix As SAPbouiCOM.Matrix = oForm.Items.Item("19").Specific
                                Dim orset As SAPbobsCOM.Recordset = p_oDICompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                                orset.DoQuery("SELECT attachpath from oadp")
                                fillopen()
                                If String.IsNullOrEmpty(orset.Fields.Item("attachpath").Value) Then
                                    p_oSBOApplication.StatusBar.SetText("Select the Attachment folder path in the General Settings", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
                                    BubbleEvent = False
                                    Exit Sub
                                End If
                                If p_sSelectedFileName <> "" Then
                                    Dim file = New FileInfo(p_sSelectedFilepath)
                                    file.CopyTo(Path.Combine(orset.Fields.Item("attachpath").Value, file.Name), True)
                                    oMatrix = oForm.Items.Item("19").Specific
                                    If oMatrix.RowCount = 0 Then
                                        oMatrix.AddRow()
                                    Else
                                        If oMatrix.Columns.Item("V_0").Cells.Item(oMatrix.RowCount).Specific.String <> "" And oMatrix.Columns.Item("V_1").Cells.Item(oMatrix.RowCount).Specific.String <> "" Then
                                            oMatrix.AddRow()
                                        End If
                                    End If

                                    oMatrix.Columns.Item("V_-1").Cells.Item(oMatrix.RowCount).Specific.string = oMatrix.RowCount
                                    oMatrix.Columns.Item("V_1").Cells.Item(oMatrix.RowCount).Specific.string = p_sSelectedFileName
                                    oMatrix.Columns.Item("V_2").Cells.Item(oMatrix.RowCount).Specific.string = orset.Fields.Item("attachpath").Value & p_sSelectedFileName
                                    oMatrix.Columns.Item("V_0").Cells.Item(oMatrix.RowCount).Specific.string = PostDate(p_oDICompany)
                                    'oMatrix.AutoResizeColumns()
                                    p_sSelectedFileName = ""
                                    p_sSelectedFilepath = ""

                                End If

                                If oForm.Mode = SAPbouiCOM.BoFormMode.fm_OK_MODE Then
                                    oForm.Mode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE
                                End If

                                oForm.Freeze(True)
                                oMatrix.AutoResizeColumns()
                                oForm.Freeze(False)
                                If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed With Success File Open Function", sFuncName)
                                ' oForm.Items.Item("Item_5").Specific.string = p_sSelectedFilepath
                                Exit Sub
                            End If

                            If pVal.ItemUID = "1" And (pVal.FormMode = SAPbouiCOM.BoFormMode.fm_OK_MODE) Then
                                Dim oForm As SAPbouiCOM.Form = p_oSBOApplication.Forms.ActiveForm
                                Dim oMAtrix As SAPbouiCOM.Matrix = oForm.Items.Item("Item_3").Specific
                                If Not String.IsNullOrEmpty(oMAtrix.Columns.Item("Col_0").Cells.Item(oMAtrix.RowCount).Specific.ToString) Then
                                    p_iRowCount = oMAtrix.RowCount - 1
                                Else
                                    p_iRowCount = oMAtrix.RowCount
                                End If

                                oForm.Items.Item("Item_13").Enabled = False
                                Exit Sub
                            End If

                            If pVal.ItemUID = "1" And pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Then
                                Dim oForm As SAPbouiCOM.Form = p_oSBOApplication.Forms.ActiveForm
                                If pVal.Action_Success = True Then
                                    Dim oMatrix As SAPbouiCOM.Matrix = oForm.Items.Item("Item_3").Specific
                                    StockBudget_Binding(oForm, p_oDICompany, p_oSBOApplication, sErrDesc)
                                    oMatrix.Columns.Item("Col_7").Cells.Item(1).Specific.String = 0
                                    oMatrix.Columns.Item("Col_8").Cells.Item(1).Specific.String = 0
                                    oMatrix.Columns.Item("Col_9").Cells.Item(1).Specific.String = 0
                                    oMatrix.AutoResizeColumns()
                                End If
                            End If

                        End If

                        If pVal.EventType = SAPbouiCOM.BoEventTypes.et_CHOOSE_FROM_LIST Then
                            Dim oCFLEvento As SAPbouiCOM.IChooseFromListEvent
                            oCFLEvento = pVal
                            Dim sCFL_ID As String
                            sCFL_ID = oCFLEvento.ChooseFromListUID
                            Dim oForm As SAPbouiCOM.Form = p_oSBOApplication.Forms.Item(FormUID)
                            Dim oCFL As SAPbouiCOM.ChooseFromList
                            oCFL = oForm.ChooseFromLists.Item(sCFL_ID)
                            If oCFLEvento.BeforeAction = False Then
                                Dim oDataTable As SAPbouiCOM.DataTable
                                Dim oMatrix As SAPbouiCOM.Matrix = oForm.Items.Item("Item_3").Specific
                                oDataTable = oCFLEvento.SelectedObjects
                                If pVal.ItemUID = "Item_3" And pVal.ColUID = "Col_0" Then
                                    Try
                                        If oMatrix.RowCount = pVal.Row Then
                                            oMatrix.AddRow()
                                            oMatrix.Columns.Item("#").Cells.Item(oMatrix.RowCount).Specific.String = oMatrix.RowCount
                                        End If
                                        oMatrix.Columns.Item("Col_1").Cells.Item(pVal.Row).Specific.String = oDataTable.GetValue("ItemName", 0)
                                        oMatrix.Columns.Item("Col_0").Cells.Item(pVal.Row).Specific.String = oDataTable.GetValue("ItemCode", 0)

                                    Catch ex As Exception
                                    End Try
                                End If

                                If pVal.ItemUID = "Item_3" And pVal.ColUID = "Col_3" Then
                                    Try
                                        If oMatrix.RowCount = pVal.Row Then
                                            oMatrix.AddRow()
                                            oMatrix.Columns.Item("#").Cells.Item(oMatrix.RowCount).Specific.String = oMatrix.RowCount
                                        End If
                                        oMatrix.Columns.Item("Col_10").Cells.Item(pVal.Row).Specific.String = oDataTable.GetValue("WhsName", 0)
                                        oMatrix.Columns.Item("Col_3").Cells.Item(pVal.Row).Specific.String = oDataTable.GetValue("WhsCode", 0)

                                    Catch ex As Exception
                                    End Try
                                End If
                            End If
                        End If
                End Select

                Select Case pVal.FormTypeEx

                    Case "720"
                        If pVal.EventType = SAPbouiCOM.BoEventTypes.et_FORM_LOAD Then
                            Dim oForm As SAPbouiCOM.Form = p_oSBOApplication.Forms.GetForm(pVal.FormType, 0)

                            If CreateButton_GoodsIssue(oForm, sErrDesc) <> RTN_SUCCESS Then Throw New ArgumentException(sErrDesc)

                        End If

                        If pVal.EventType = SAPbouiCOM.BoEventTypes.et_CLICK Then

                            If pVal.ItemUID = "btnCpyFrm" Then
                                Dim oForm As SAPbouiCOM.Form
                                LoadFromXML("PRList.srf", SBO_Application)
                                oForm = SBO_Application.Forms.Item("PR")

                                oForm.Visible = True

                                Display_PurchaseRequest(oForm, "", sErrDesc)

                            End If

                        End If

                    Case "940"
                        If pVal.EventType = SAPbouiCOM.BoEventTypes.et_FORM_LOAD Then
                            Dim oForm As SAPbouiCOM.Form = p_oSBOApplication.Forms.GetForm(pVal.FormType, 0)

                            If CreateButton_InventoryTransfer(oForm, sErrDesc) <> RTN_SUCCESS Then Throw New ArgumentException(sErrDesc)

                        End If

                        If pVal.EventType = SAPbouiCOM.BoEventTypes.et_CLICK Then

                            If pVal.ItemUID = "btnCpyFrm" Then
                                Dim oForm As SAPbouiCOM.Form
                                LoadFromXML("PRList.srf", SBO_Application)
                                oForm = SBO_Application.Forms.Item("PR")

                                oForm.Visible = True

                                Display_PurchaseRequest(oForm, "", sErrDesc)

                            End If

                        End If
 

                    Case "60004"
                        If pVal.EventType = SAPbouiCOM.BoEventTypes.et_CLICK Then
                            Dim oForm As SAPbouiCOM.Form = p_oSBOApplication.Forms.GetForm(pVal.FormType, 0)
                            If pVal.ItemUID = "btnChoose" Then
                                GetSelectedRows(oForm, sErrDesc)
                                oForm.Items.Item("2").Click(SAPbouiCOM.BoCellClickType.ct_Regular)

                                If p_oSBOApplication.Forms.ActiveForm.TypeEx = "720" Then
                                    If Fill_MatrixValues_GoodsIssue(p_oSBOApplication.Forms.ActiveForm, sErrDesc) <> RTN_SUCCESS Then Throw New ArgumentException(sErrDesc)
                                ElseIf p_oSBOApplication.Forms.ActiveForm.TypeEx = "940" Then
                                    If Fill_MatrixValues_InventoryTransfer(p_oSBOApplication.Forms.ActiveForm, sErrDesc) <> RTN_SUCCESS Then Throw New ArgumentException(sErrDesc)
                                End If



                            End If
                        End If

                        If pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED Then
                            Dim oCheckBox As SAPbouiCOM.CheckBox
                            Dim oForm As SAPbouiCOM.Form = p_oSBOApplication.Forms.GetForm(pVal.FormType, 0)
                            oCheckBox = oForm.Items.Item("4").Specific

                            If pVal.ItemUID = "4" Then

                                If oCheckBox.Checked = True Then
                                    Display_PurchaseRequest(oForm, "Y", sErrDesc)
                                ElseIf oCheckBox.Checked = False Then
                                    Display_PurchaseRequest(oForm, "", sErrDesc)
                                End If

                            End If
                        End If

                End Select
            Else
                Select Case pVal.FormTypeEx

                    Case "2000054" ' Projects

                        If pVal.EventType = SAPbouiCOM.BoEventTypes.et_CLICK Then
                            If pVal.ItemUID = "4" And (pVal.ColUID = "COL1" Or pVal.ColUID = "COL2") Then
                                Dim oForm As SAPbouiCOM.Form = p_oSBOApplication.Forms.ActiveForm
                                Try
                                    Dim oMatrix As SAPbouiCOM.Matrix = oForm.Items.Item("4").Specific
                                    sProjectName = oMatrix.Columns.Item("COL2").Cells.Item(pVal.Row).Specific.String
                                Catch ex As Exception
                                    p_oSBOApplication.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                    BubbleEvent = False
                                    Exit Sub
                                End Try
                            End If

                            If pVal.ItemUID = "1" Then
                                Dim oForm As SAPbouiCOM.Form = p_oSBOApplication.Forms.Item("SBS")
                                Try
                                    oForm.Items.Item("Item_15").Specific.String = sProjectName
                                Catch ex As Exception
                                    p_oSBOApplication.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                    BubbleEvent = False
                                    Exit Sub
                                End Try
                            End If
                        End If

                        If pVal.EventType = SAPbouiCOM.BoEventTypes.et_DOUBLE_CLICK Then
                            If pVal.ItemUID = "4" And (pVal.ColUID = "COL1" Or pVal.ColUID = "COL2") Then
                                Dim oForm As SAPbouiCOM.Form = p_oSBOApplication.Forms.ActiveForm
                                Try
                                    Dim oMatrix As SAPbouiCOM.Matrix = oForm.Items.Item("4").Specific
                                    sProjectName = oMatrix.Columns.Item("COL2").Cells.Item(pVal.Row).Specific.String
                                    oForm = p_oSBOApplication.Forms.Item("SBS")
                                    oForm.Items.Item("Item_15").Specific.String = sProjectName
                                Catch ex As Exception
                                    p_oSBOApplication.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                    BubbleEvent = False
                                    Exit Sub
                                End Try
                            End If
                        End If
                End Select
                Select Case pVal.FormUID
                    Case "SBS"
                        If pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED And pVal.FormMode <> SAPbouiCOM.BoFormMode.fm_FIND_MODE Then

                            If pVal.ItemUID = "23" Then
                                Dim oForm As SAPbouiCOM.Form = p_oSBOApplication.Forms.ActiveForm
                                Dim omatrix As SAPbouiCOM.Matrix = oForm.Items.Item("19").Specific
                                For imjs As Integer = 1 To omatrix.RowCount
                                    If omatrix.IsRowSelected(imjs) = True Then
                                        Process.Start(omatrix.Columns.Item("V_2").Cells.Item(imjs).Specific.String)
                                    End If
                                Next imjs
                            End If

                            If pVal.ItemUID = "21" Then
                                Dim oForm As SAPbouiCOM.Form = p_oSBOApplication.Forms.ActiveForm
                                Try

                                    Dim oMatrix As SAPbouiCOM.Matrix = oForm.Items.Item("19").Specific
                                    Dim rowcount As Integer = oMatrix.RowCount
                                    Dim DeleteFlag As Boolean = False
                                    If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Starting Deleting Attachment File ", sFuncName)
                                    For mjs As Integer = 1 To rowcount
                                        If mjs <= oMatrix.RowCount Then
                                            If oMatrix.IsRowSelected(mjs) = True Then
                                                oMatrix.DeleteRow(mjs)
                                                DeleteFlag = True
                                                mjs = mjs - 1
                                            End If
                                        Else
                                            Exit For
                                        End If
                                    Next mjs

                                    If DeleteFlag = True Then
                                        For mjs As Integer = 1 To oMatrix.RowCount
                                            oMatrix.Columns.Item("V_-1").Cells.Item(mjs).Specific.string = mjs
                                        Next
                                        If oForm.Mode = SAPbouiCOM.BoFormMode.fm_OK_MODE Then
                                            oForm.Mode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE
                                        End If
                                    End If
                                    If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed Deleting Attachment File ", sFuncName)
                                Catch ex As Exception
                                    p_oSBOApplication.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                    BubbleEvent = False
                                    sErrDesc = ex.Message
                                    If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR", sFuncName)
                                    WriteToLogFile(Err.Description, sFuncName)
                                End Try

                                Exit Sub
                            End If

                            If pVal.ItemUID = "14" Or pVal.ItemUID = "15" Then
                                Dim oForm As SAPbouiCOM.Form = p_oSBOApplication.Forms.ActiveForm
                                Select Case pVal.ItemUID

                                    Case "14"
                                        oForm.PaneLevel = 1
                                    Case "15"
                                        oForm.PaneLevel = 2
                                End Select
                            End If

                            If pVal.ItemUID = "1" And (pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Or pVal.FormMode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE) Then

                                Dim oForm As SAPbouiCOM.Form = p_oSBOApplication.Forms.ActiveForm
                                Dim oDocDatatable As DataTable = Nothing
                                Dim oDisplay As DataTable = Nothing
                                Dim oMatrix As SAPbouiCOM.Matrix = oForm.Items.Item("Item_3").Specific
                                Dim irow As Integer = 0
                                Dim oEdit As SAPbouiCOM.EditText
                                sErrDesc = String.Empty
                                Dim sDocEntry As String = String.Empty
                                sFuncName = "Add Button Click()"
                                Try
                                    If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Starting Function ", sFuncName)

                                    If oForm.Mode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE Then
                                        sDocEntry = oForm.Items.Item("1000002").Specific.String
                                        If Not String.IsNullOrEmpty(oMatrix.Columns.Item("Col_0").Cells.Item(oMatrix.RowCount).Specific.string) Then
                                            irow = oMatrix.RowCount
                                        Else
                                            irow = oMatrix.RowCount - 1
                                        End If

                                        If irow = p_iRowCount Then Exit Sub
                                    Else
                                        sDocEntry = 0
                                    End If



                                    If String.IsNullOrEmpty(oForm.Items.Item("Item_14").Specific.String) Then
                                        oForm.Items.Item("Item_14").Specific.active = True
                                        p_oSBOApplication.StatusBar.SetText("Project should not be Empty .... ! ", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
                                        BubbleEvent = False
                                        Exit Sub
                                    End If

                                    If String.IsNullOrEmpty(oForm.Items.Item("Item_17").Specific.String) Then
                                        oForm.Items.Item("Item_17").Specific.active = True
                                        p_oSBOApplication.StatusBar.SetText("Profit Center should not be Empty .... ! ", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
                                        BubbleEvent = False
                                        Exit Sub
                                    End If

                                    If oMatrix.RowCount = 1 Then
                                        If String.IsNullOrEmpty(oMatrix.Columns.Item("Col_0").Cells.Item(1).Specific.String) Then
                                            ' oEdit = oMatrix.Columns.Item("Col_0").Cells.Item(1).Specific
                                            'oEdit.Active = True
                                            p_oSBOApplication.StatusBar.SetText("Item Code should not be Empty .... ! ", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
                                            BubbleEvent = False
                                            Exit Sub
                                        End If
                                        If String.IsNullOrEmpty(oMatrix.Columns.Item("Col_2").Cells.Item(1).Specific.String) Then
                                            ' oMatrix.Columns.Item("Col_2").Cells.Item(1).Specific.active = True
                                            p_oSBOApplication.StatusBar.SetText("Budget Month should not be Empty .... !", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
                                            BubbleEvent = False
                                            Exit Sub
                                        End If
                                        If String.IsNullOrEmpty(oMatrix.Columns.Item("Col_3").Cells.Item(1).Specific.String) Then
                                            ' oMatrix.Columns.Item("Col_3").Cells.Item(1).Specific.active = True
                                            p_oSBOApplication.StatusBar.SetText("Warehouse should not be Empty .... !", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
                                            BubbleEvent = False
                                            Exit Sub
                                        End If
                                        ''If String.IsNullOrEmpty(oMatrix.Columns.Item("Col_6").Cells.Item(1).Specific.String) Then
                                        ''    If CInt(oMatrix.Columns.Item("Col_6").Cells.Item(1).Specific.String) < 0 Then
                                        ''        p_oSBOApplication.StatusBar.SetText("Total should not be zero / less then zero .... ! ", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
                                        ''        BubbleEvent = False
                                        ''        Exit Sub
                                        ''    End If
                                        ''End If
                                    End If

                                    For imjs As Integer = 1 To oMatrix.RowCount - 1
                                        If String.IsNullOrEmpty(oMatrix.Columns.Item("Col_0").Cells.Item(imjs).Specific.String) Then
                                            ' oMatrix.Columns.Item("Col_0").Cells.Item(imjs).Specific.active = True
                                            p_oSBOApplication.StatusBar.SetText("Item Code should not be Empty .... ! " & imjs, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
                                            BubbleEvent = False
                                            Exit Sub
                                        End If
                                        If String.IsNullOrEmpty(oMatrix.Columns.Item("Col_2").Cells.Item(imjs).Specific.String) Then
                                            ' oMatrix.Columns.Item("Col_2").Cells.Item(imjs).Specific.active = True
                                            p_oSBOApplication.StatusBar.SetText("Budget Month should not be Empty .... !" & imjs, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
                                            BubbleEvent = False
                                            Exit Sub
                                        End If
                                        If String.IsNullOrEmpty(oMatrix.Columns.Item("Col_3").Cells.Item(imjs).Specific.String) Then
                                            'oMatrix.Columns.Item("Col_3").Cells.Item(imjs).Specific.active = True
                                            p_oSBOApplication.StatusBar.SetText("Warehouse should not be Empty .... !" & imjs, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
                                            BubbleEvent = False
                                            Exit Sub
                                        End If
                                        ''If String.IsNullOrEmpty(oMatrix.Columns.Item("Col_6").Cells.Item(imjs).Specific.String) Then
                                        ''    p_oSBOApplication.StatusBar.SetText("Total should not be zero / less then zero .... ! " & imjs, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
                                        ''    BubbleEvent = False
                                        ''    Exit Sub
                                        ''Else
                                        ''    If CInt(oMatrix.Columns.Item("Col_6").Cells.Item(imjs).Specific.String) < 0 Then
                                        ''        p_oSBOApplication.StatusBar.SetText("Total should not be zero / less then zero .... ! " & imjs, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
                                        ''        BubbleEvent = False
                                        ''        Exit Sub
                                        ''    End If
                                        ''End If

                                    Next imjs

                                    If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling MatrixDataToDataTable()", sFuncName)

                                    oDocDatatable = MatrixDataToDataTable(oMatrix, oForm, sErrDesc)
                                    If Not String.IsNullOrEmpty(sErrDesc) Then
                                        BubbleEvent = False
                                        Exit Sub
                                    End If
                                    If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling ProjectValidation() ", sFuncName)
                                    oDisplay = ProjectValidation(oDocDatatable, p_oDICompany, sDocEntry, sErrDesc)
                                    If Not String.IsNullOrEmpty(sErrDesc) Then
                                        BubbleEvent = False
                                        Exit Sub
                                    End If
                                    If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling Write_TextFile() ", sFuncName)
                                    If oDisplay.Rows.Count > 0 Then
                                        Write_TextFile(oDisplay, sErrDesc)
                                        BubbleEvent = False
                                        Exit Sub
                                    End If

                                    If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS ", sFuncName)
                                    'ProjectValidation
                                Catch ex As Exception
                                    p_oSBOApplication.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                    BubbleEvent = False
                                    If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR", sFuncName)
                                    WriteToLogFile(Err.Description, sFuncName)
                                End Try


                            End If
                        End If

                        If pVal.EventType = SAPbouiCOM.BoEventTypes.et_VALIDATE And pVal.InnerEvent = False Then
                            If pVal.ItemUID = "Item_3" And (pVal.ColUID = "Col_4" Or pVal.ColUID = "Col_5") Then
                                Dim oForm As SAPbouiCOM.Form = p_oSBOApplication.Forms.ActiveForm
                                Dim oMatrix As SAPbouiCOM.Matrix = oForm.Items.Item("Item_3").Specific

                                Try
                                    oForm.Freeze(True)
                                    If String.IsNullOrEmpty(oMatrix.Columns.Item(pVal.ColUID).Cells.Item(pVal.Row).Specific.String) Then
                                        oMatrix.Columns.Item(pVal.ColUID).Cells.Item(pVal.Row).Specific.String = 0
                                    End If
                                    If oForm.Mode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Then
                                        oMatrix.Columns.Item("Col_9").Cells.Item(pVal.Row).Specific.String = 0
                                        oMatrix.Columns.Item("Col_9").Cells.Item(pVal.Row).Specific.String = 0
                                        oMatrix.Columns.Item("Col_9").Cells.Item(pVal.Row).Specific.String = oMatrix.Columns.Item("Col_4").Cells.Item(pVal.Row).Specific.String

                                    ElseIf oForm.Mode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE Then
                                        If CDbl(oMatrix.Columns.Item("Col_7").Cells.Item(pVal.Row).Specific.String) > CDbl(oMatrix.Columns.Item("Col_4").Cells.Item(pVal.Row).Specific.String) Then
                                            oMatrix.Columns.Item("Col_4").Cells.Item(pVal.Row).Specific.active = True
                                            p_oSBOApplication.StatusBar.SetText("Budget Qty cannot be less than PR Qty.", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
                                            oForm.Freeze(False)
                                            BubbleEvent = False
                                            Exit Sub
                                        Else
                                            oMatrix.Columns.Item("Col_9").Cells.Item(pVal.Row).Specific.String = CDbl(oMatrix.Columns.Item("Col_4").Cells.Item(pVal.Row).Specific.String) - CDbl(oMatrix.Columns.Item("Col_7").Cells.Item(pVal.Row).Specific.String)
                                        End If
                                    End If
                                    oMatrix.Columns.Item("Col_6").Cells.Item(pVal.Row).Specific.String = CDbl(oMatrix.Columns.Item("Col_4").Cells.Item(pVal.Row).Specific.String) * CDbl(oMatrix.Columns.Item("Col_5").Cells.Item(pVal.Row).Specific.String)
                                    oMatrix.AutoResizeColumns()

                                    oForm.Freeze(False)
                                Catch ex As Exception
                                    oForm.Freeze(False)
                                    p_oSBOApplication.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                    BubbleEvent = False
                                    If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR", sFuncName)
                                    WriteToLogFile(Err.Description, sFuncName)
                                End Try
                            End If
                        End If


                End Select
            End If


            'If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS", sFuncName)
        Catch exc As Exception
            p_oSBOApplication.StatusBar.SetText(exc.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
            BubbleEvent = False
            sErrDesc = exc.Message
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR", sFuncName)
            WriteToLogFile(Err.Description, sFuncName)
            ShowErr(sErrDesc)
        End Try

    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub

    Private Sub SBO_Application_RightClickEvent(ByRef eventInfo As SAPbouiCOM.ContextMenuInfo, ByRef BubbleEvent As Boolean) Handles SBO_Application.RightClickEvent
        Dim oform As SAPbouiCOM.Form = p_oSBOApplication.Forms.ActiveForm

        If oform.UniqueID = "SBS" Then
            Dim oMenuItem As SAPbouiCOM.MenuItem
            Dim oMenus As SAPbouiCOM.Menus
            ' Create menu popup MyUserMenu01 and add it to Tools menu
            Dim oCreationPackage As SAPbouiCOM.MenuCreationParams
            oCreationPackage = p_oSBOApplication.CreateObject(SAPbouiCOM.BoCreatableObjectType.cot_MenuCreationParams)
            oMenuItem = p_oSBOApplication.Menus.Item("1280") 'Data'
            oMenus = oMenuItem.SubMenus
            'Create sub menu MySubMenu1 and add it to popup MyUserMenu01
            oCreationPackage.Type = SAPbouiCOM.BoMenuType.mt_STRING
            oCreationPackage.UniqueID = "3045646"
            oCreationPackage.String = "Delete Row"
            oCreationPackage.Enabled = True
            If Not p_oSBOApplication.Menus.Exists("3045646") Then
                'If the menu already exists this code will fail
                oMenus.AddEx(oCreationPackage)
            End If
            ''oMenus.AddEx(oCreationPackage)
        End If
    End Sub
End Class
